package LinkedList.Two_Way;

public class Two_wayLinkedList {//双向链表
    static class ListNode{
        public int value;
        public ListNode next;//后继节点的地址
        public ListNode prev;//前驱节点的地址

        public ListNode(int value) {
            this.value = value;
        }
    }

    public ListNode head;//双向链表的头节点
    public ListNode last;//双向链表的尾节点

    //链表的长度
    public int size() {
        int count = 0;
        ListNode cur =head;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //输出双向链表
    public void display() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.value + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //是否包含某一个数字
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.value == key)
                return true;
            cur = cur.next;
        }
        return false;
    }

    public void addFirst(int data) {//头插法
        ListNode node = new ListNode(data);
        if(head == null) {//如果是一个空链表
            head = node;
            last = node;
        } else {
            node.next = head;
            head.prev = node;
            head = node;
        }
    }

    public void addLast(int data) {//头插法
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
            last = node;
        } else {
            last.next = node;
            node.prev = last;
            last = last.next;
        }
    }

    //在指定位置插入
    public void addIndex(int index, int data) {
        checkIndex(index);//判断index是否合法
        ListNode node = new ListNode(data);
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == size()) {
            addLast(data);
            return;
        }
        ListNode cur = head;
        for (int i = 0; i < index; i++) {
            cur = cur.next;//让cur走到index的位置
        }
        //插入节点
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }

    private void checkIndex(int index) {
        if(index < 0 || index > size()) {
            throw new IndexOutOfRangeException("index不合法！" + index);
        }
    }
    //删除第一次出现值为key的节点
    public void remove(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.value == key) {
                //删除头节点
                if(cur == head) {
                    head = head.next;
                    if(head != null) {
                        //只有一个节点
                        head.prev = null;
                    } else {
                        last = null;
                    }
                } else {
                    if(cur.next != null) {
                        //删除中间节点
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    } else {
                        //删除尾节点
                        cur.prev.next = cur.next;
                        last = last.prev;
                    }
                }

            } else {
                cur = cur.next;
            }
        }
    }

    //置空
    public void clear() {
        ListNode cur = head;
        while(cur != null) {
            ListNode tmp = cur.next;//后面置为空的时候，会找不到cur.next要记录
            cur.prev = null;
            cur.next = null;
            cur = tmp;
        }
        head = null;
        last = null;
    }
}
